package cp.server.jobs;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import cp.server.common.Comment;
import cp.server.common.HotComment;
import cp.server.common.SystemProperty;
import cp.server.dao.ServerDAO;
import cp.server.util.ConnectionFactory;
import cp.server.util.FileUtils;
import cp.server.util.FtpUtils;
import cp.server.util.SFtpUtils;

public class RealTimeHotCommentFinder
{
    private FtpUtils ftp = new FtpUtils();

    public void go()
    {
        ServerDAO dao = new ServerDAO();
        List<HotComment> list;
        List<Comment> latestList;
        List<Comment> hotList;
        Connection con = null;

        try
        {
            con = ConnectionFactory.getConnection();
            ftp.connectServerDefault();
            list = dao.queryRealTimeHotComment(con);
            FileUtils.writeOjectToFile(
                    SystemProperty.RealTimeHotCommentFileName, list);

            ftp.uploadFile(SystemProperty.RealTimeHotCommentFileName);

            try
            {
                SFtpUtils.upload(SystemProperty.OPENSHIFTHOSTNAME,
                        SystemProperty.OPENSHIFTUSERNAME, "",
                        SystemProperty.RealTimeHotCommentFileName,
                        SystemProperty.OPENSHIFTREMOTEFILEPATH
                                + SystemProperty.RealTimeHotCommentFileName,
                        SystemProperty.OPENSHIFTPPKPATH,
                        SystemProperty.OPENSHIFTPPKPWD);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

            SFtpUtils.upload(SystemProperty.HUAWEIHOSTNAME,
                    SystemProperty.HUAWEIUSERNAME,
                    SystemProperty.HUAWEIPASSWORD,
                    SystemProperty.RealTimeHotCommentFileName,
                    SystemProperty.HUAWEIREMOTEFILEPATH
                            + SystemProperty.RealTimeHotCommentFileName, null, null);

            for (HotComment cmt : list)
            {
                String pageId = cmt.getPageId();
                String pageDate = cmt.getPageDate().toString();

                hotList = dao.queryCommentByPageIdOrderByAgree(con, pageId);
                latestList = dao.queryCommentByPageIdOrderByTime(con, pageId);

                FileUtils.writeOjectToFile(SystemProperty.HotListFileName + "_"
                        + pageDate + "_" + pageId, hotList);
                FileUtils.writeOjectToFile(SystemProperty.LatestListFileName
                        + "_" + pageDate + "_" + pageId, latestList);

                ftp.uploadFile(SystemProperty.HotListFileName + "_" + pageDate
                        + "_" + pageId);
                ftp.uploadFile(SystemProperty.LatestListFileName + "_"
                        + pageDate + "_" + pageId);

            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (con != null)
                {
                    con.close();
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

            if (ftp != null)
            {
                try
                {
                    ftp.disConnect();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String args[])
    {
        RealTimeHotCommentFinder r = new RealTimeHotCommentFinder();
        r.go();
    }
}
